package cn.lixunims.ims.modular.report;

import cn.hutool.json.JSONObject;
import cn.lixunims.ims.modular.bill.entity.ImsBillDetail;
import cn.lixunims.ims.modular.bill.param.ImsBillAddParam;
import cn.lixunims.ims.modular.bill.param.ImsBillHeaderIdParam;
import cn.lixunims.ims.modular.bill.service.ImsBillHeaderService;
import cn.lixunims.ims.modular.order.param.ImsOrderAddParam;
import cn.lixunims.ims.modular.order.param.ImsOrderHeaderIdParam;
import cn.lixunims.ims.modular.order.service.ImsOrderHeaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/ims/report")
public class BillReportController {

    @Autowired
    ImsBillHeaderService billHeaderService;
    @Autowired
    ImsOrderHeaderService orderHeaderService;
    // 单据
    @GetMapping("/header")
    public JSONObject purchaseHeader(String id) {
        ImsBillHeaderIdParam imsBillHeaderIdParam = new ImsBillHeaderIdParam();
        imsBillHeaderIdParam.setBillId(id);
        ImsBillAddParam detail = billHeaderService.detail(imsBillHeaderIdParam);
        JSONObject entries = new JSONObject();
        entries.put("data", detail.getHeader());
        return entries;
    }
    @GetMapping("/record")
    public JSONObject purchaseRecord(String id) {
        ImsBillHeaderIdParam imsBillHeaderIdParam = new ImsBillHeaderIdParam();
        imsBillHeaderIdParam.setBillId(id);
        ImsBillAddParam detail = billHeaderService.detail(imsBillHeaderIdParam);
        // 给清单添加序号
        for (int i = 0; i < detail.getRecordList().size(); i++) {
            detail.getRecordList().get(i).setNo(i + 1);
        }

        JSONObject entries = new JSONObject();
        entries.put("data", detail.getRecordList());
        return entries;
    }
    // 订单
    @GetMapping("/headerOrder")
    public JSONObject purchaseHeaderOrder(String id) {
        ImsOrderHeaderIdParam imsBillHeaderIdParam = new ImsOrderHeaderIdParam();
        imsBillHeaderIdParam.setBillId(id);
        ImsOrderAddParam detail = orderHeaderService.detail(imsBillHeaderIdParam);
        JSONObject entries = new JSONObject();
        entries.put("data", detail.getHeader());
        return entries;
    }
    @GetMapping("/recordOrder")
    public JSONObject purchaseRecordOrder(String id) {
        ImsOrderHeaderIdParam imsBillHeaderIdParam = new ImsOrderHeaderIdParam();
        imsBillHeaderIdParam.setBillId(id);
        ImsOrderAddParam detail = orderHeaderService.detail(imsBillHeaderIdParam);
        // 给清单添加序号
        for (int i = 0; i < detail.getRecordList().size(); i++) {
            detail.getRecordList().get(i).setNo(i + 1);
        }
        JSONObject entries = new JSONObject();
        entries.put("data", detail.getRecordList());
        return entries;
    }

}
